<?php 
require("gConfig.php");
$uid = $_GET['uid'];
if ( $uid == "" )
{
    exit( );
}
$room = $_GET["room"];
if ($room == "") {
	exit( );
}
if (!isset( $room )) {
	exit( );
}
$Lang = $_GET["Lang"];
if (!isset( $Lang )) {
	$Lang = "utf-8";
}
checkuid($uid,5);

$errstr = "";

$resule = mysql_query("select sState from  m_state where sId= '$room' Limit 1",$comm);
$row = mysql_fetch_row($resule);
$state = $row[0];

$resule = mysql_query("select * from  m_users where uName=".$MyData['MyName']." Limit 1",$comm);
if(1 <= mysql_num_rows($resule)){
	$row = mysql_fetch_row($resule);
//	error_log("start game log row is:".print_r($row,true));
	if ($row[0] == $room){
		$msg =  "已经在大厅{$room}中！";
	}else{
		if($state != 4){
			if($row[3] == "0,0,0,0,0,0,0"){
				$resule = mysql_query("select gBet,gBetWin,gCredit from  b_group where gName=".$MyData['Name'] ." Limit 1",$comm);
				$row = mysql_fetch_row($resule);
				mysql_query("update m_users set uBet={$row[0]}, uWin={$row[1]}, uCredit={$row[2]} ,uId={$room} ",comm);
			}else{
				$resule = mysql_query("select gBet,gBetWin,gCredit from  b_group where gName=".$MyData['Name'] ." Limit 1",$comm);
				$row = mysql_fetch_row($resule);
				mysql_query("update m_users set uBet={$row[0]}, uWin={$row[1]}, uCredit={$row[2]} ",comm);
				$msg = "上一场游戏还没有结束，已登录房间$row[0]继续游戏。";
			}
		}else{
			$errstr = "当前游戏房间已经停止服务，不能登录";
		}
	}
}else{
	if($state != 4){
		$resule = mysql_query("select gTree,gName,gBet,gBetWin,gCredit,gUse,gState,gGid from  b_group where gName=".$MyData['Name'] ." Limit 1",$comm);
		$row = mysql_fetch_row($resule);
		$trees = explode(",", $row[0]);
		$uGroup = "@".$trees[1]."@".$trees[2]."@".$trees[3]."@".$trees[4];
		$state = $row[6];
		$gid = $row[7];
		if($state & 32){
			$query = "select gRang,gRang1 from b_group where gName = '$trees[4]'";
			$newresult = mysql_query($query,$comm);
			$newrow = mysql_fetch_row($newresult);
			$pes = intval($row[0])+intval($row[1]);
			$uGroup .= "@{$pes}@{$gid}@";
		}else{
			$uGroup .= "@0@{$gid}@";
		}
		
		$insertstr = "insert into  m_users(uId,uGroup,uName,uBets,uWin,uBet,uCredit) 
		values ('$room','$uGroup','$row[1]','0,0,0,0,0,0,0',$row[3],$row[2],$row[4])";
// 		error_log("start game log insertstr is:".print_r($insertstr,true));
// 		error_log("start game log mysql_error is:".print_r(mysql_error(),true));
		mysql_query($insertstr,$comm);
		$msg =  "登录大厅{$room}成功！";
	}else{
		$errstr = "当前游戏房间不允许登陆";
	}
}

function loadSWF()
{
	global $uid;
	global $Lang;
	global $room;
	global $MyData;
	$ipaddr="http://hongshon.eicp.net:54067";
	$parastr="uid=".$uid."&Lang=".$Lang."&room=".$room."&ipaddr=".$ipaddr."&uname=".$MyData['MyName'];
	echo "<object classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" width=\"1280\" height=\"1024\" id=\"casino\">\n";
	echo "<param name=\"movie\" value=\"casino.swf\" />\n";
	echo "<param name=\"FlashVars\" value=\"".$parastr."\" />\n";
	echo "<param name=\"quality\" value=\"high\" />\n";
	echo "<param name=\"bgcolor\" value=\"#ffffff\" />\n";
	echo "<param name=\"allowScriptAccess\" value=\"sameDomain\" />\n";
	echo "<param name=\"allowFullScreen\" value=\"true\" />\n";
	echo "<object type=\"application/x-shockwave-flash\" data=\"casino.swf\" width=\"1280\" height=\"1024\">\n";
	echo "<param name=\"quality\" value=\"high\" />\n";
	echo "<param name=\"FlashVars\" value=\"".$parastr."\" />\n";
	echo "<param name=\"bgcolor\" value=\"#ffffff\" />\n";
	echo "<param name=\"allowScriptAccess\" value=\"sameDomain\" />\n";
	echo "<param name=\"allowFullScreen\" value=\"true\" />\n";
	echo " <!--<![endif]-->\n";
	echo "<!--[if gte IE 6]>-->\n";
	echo " <p> \n";
	echo "      Either scripts and active content are not permitted to run or Adobe Flash Player version\n";
	echo "      11.1.0 or greater is not installed.\n";
	echo "</p><!--<![endif]--><a href=\"http://www.adobe.com/go/getflashplayer\">\n";
	echo "<img src=\"http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif\" alt=\"Get Adobe Flash Player\" />\n";
	echo "</a><!--[if !IE]>-->\n";
	echo "</object>  <!--<![endif]-->\n";
	echo "</object>\n";

}

function TestBet() {
	global $uid;
	global $Lang;
	global $room;
	
	$str = "\r\n<br/><br/><br/><br/>\r\n";
	
	$str .= "<a href='StartGame.php?uid=$uid&Lang=$Lang&room=$room&bet=50&type=0'> 押闲加50 </a><br/>\r\n";
	$str .= "<a href='StartGame.php?uid=$uid&Lang=$Lang&room=$room&bet=100&type=0'> 押闲加100 </a><br/>\r\n";
	$str .= "<a href='StartGame.php?uid=$uid&Lang=$Lang&room=$room&bet=200&type=0'> 押闲加200 </a><br/>\r\n";
	$str .= "<a href='StartGame.php?uid=$uid&Lang=$Lang&room=$room&bet=300&type=0'> 押闲加300 </a><br/>\r\n";
	$str .= "<a href='StartGame.php?uid=$uid&Lang=$Lang&room=$room&bet=500&type=0'> 押闲加500 </a><br/>\r\n";
	
	$str .= "<a href='StartGame.php?uid=$uid&Lang=$Lang&room=$room&bet=50&type=1'> 押庄加50 </a><br/>\r\n";
	$str .= "<a href='StartGame.php?uid=$uid&Lang=$Lang&room=$room&bet=100&type=1'> 押庄加100 </a><br/>\r\n";
	$str .= "<a href='StartGame.php?uid=$uid&Lang=$Lang&room=$room&bet=200&type=1'> 押庄加200 </a><br/>\r\n";
	$str .= "<a href='StartGame.php?uid=$uid&Lang=$Lang&room=$room&bet=300&type=1'> 押庄加300 </a><br/>\r\n";
	$str .= "<a href='StartGame.php?uid=$uid&Lang=$Lang&room=$room&bet=500&type=1'> 押庄加500 </a><br/>\r\n";
	
	$str .= "<a href='StartGame.php?uid=$uid&Lang=$Lang&room=$room&bet=50&type=2'> 押庄加50 </a><br/>\r\n";
	$str .= "<a href='StartGame.php?uid=$uid&Lang=$Lang&room=$room&bet=100&type=2'> 押庄加100 </a><br/>\r\n";
	$str .= "<a href='StartGame.php?uid=$uid&Lang=$Lang&room=$room&bet=200&type=2'> 押庄加200 </a><br/>\r\n";
	$str .= "<a href='StartGame.php?uid=$uid&Lang=$Lang&room=$room&bet=300&type=2'> 押庄加300 </a><br/>\r\n";
	$str .= "<a href='StartGame.php?uid=$uid&Lang=$Lang&room=$room&bet=500&type=2'> 押庄加500 </a><br/>\r\n";
	
	$str .= "\r\n<br/><br/><br/><br/>\r\n";
	return $str;
}

function UserStateTable() {
	global $MyData;
	global $comm;
	$resule = mysql_query("select * from  m_users where uName=".$MyData['MyName']." Limit 1",$comm);
	
	$tablestr = "<table width=\"100%\" border=\"0\" bgcolor=\"#999999\" cellspacing=\"1\" cellpadding=\"0\">
		<tbody><tr bgcolor=\"#FFFFaa\">
            <td nowrap=\"\" bgcolor=\"#FFFFaa\" align=\"center\">厅别</td>
            <td nowrap=\"\" bgcolor=\"#FFFFaa\" align=\"center\">会员帐号</td>
            <td nowrap=\"\" bgcolor=\"#FFFFaa\" align=\"center\">押闲</td>
            <td nowrap=\"\" bgcolor=\"#FFFFaa\" align=\"center\">押庄</td>
            <td nowrap=\"\" bgcolor=\"#FFFFaa\" align=\"center\">和局</td>
            <td nowrap=\"\" bgcolor=\"#FFFFaa\" align=\"center\">闲对</td>
            <td nowrap=\"\" bgcolor=\"#FFFFaa\" align=\"center\">庄对</td>
			<td nowrap=\"\" bgcolor=\"#FFFFaa\" align=\"center\">押大</td>
            <td nowrap=\"\" bgcolor=\"#FFFFaa\" align=\"center\">押小</td>
            <td nowrap=\"\" bgcolor=\"#FFFFaa\" align=\"center\">总赢</td>
            <td nowrap=\"\" bgcolor=\"#FFFFaa\" align=\"center\">总押</td>
            <td nowrap=\"\" bgcolor=\"#FFFFaa\" align=\"center\">余额</td>
            <td nowrap=\"\" align=\"center\">所属群组</td>        
		</tr>";
	$row = mysql_fetch_row($resule);
	$tablestr .= "<tr>\r\n";
	$tablestr .= "<td nowrap=\"\" bgcolor=\"#FFFFFF\" align=\"center\">{$row[0]}</td>\r\n";
	$tablestr .= "<td nowrap=\"\" bgcolor=\"#FFFFFF\" align=\"center\">{$row[2]}</td>\r\n";
	$uBets = explode(",", $row[3]);
	for ($i = 0; $i < count($uBets); $i++) {
		$tablestr .= "<td nowrap=\"\" bgcolor=\"#FFFFFF\" align=\"center\">{$uBets[$i]}</td>\r\n";
	}
	$tablestr .= "<td nowrap=\"\" bgcolor=\"#FFFFFF\" align=\"center\">{$row[4]}</td>\r\n";
	$tablestr .= "<td nowrap=\"\" bgcolor=\"#FFFFFF\" align=\"center\">{$row[5]}</td>\r\n";
	$tablestr .= "<td nowrap=\"\" bgcolor=\"#FFFFFF\" align=\"center\">{$row[6]}</td>\r\n";
	$uGroup = explode("@", $row[1]);
	$tablestr .= "<td nowrap=\"\" bgcolor=\"#FFFFFF\" align=\"center\">";
	for ($i = 1; $i < $MyData['Level']; $i++) {
		$tablestr .= $uGroup[$i].",";
	}
	$tablestr .= "</td>\r\n";
	$tablestr .= "</tr>\r\n</tbody>\r\n</table>\r\n";
	
	return $tablestr;
}

echo "<html>\r\n<head>\r\n<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">\r\n";
if ( $errstr != "")
{
    echo "<script Language='JavaScript'>";
    echo big52gb( "alert('".$errstr."');" );
    echo "\r\n</script>\r\n";
//    echo "history.back();</script>\r\n";
//    exit( );
}
echo "</head>\r\n";
echo "<body>";
echo $msg;
if($errstr == ""){
	loadSWF();
}
echo "</body>\r\n</html>";
?>